Creating a simple table of Age by Gender
Sample script file: MyFirstTable.mrs
This example opens the Museum sample data set, creates a simple table of age by gender, and exports it to an HTML file. It shows a complete script with the minimum setup required to create and export a basic table.
Note This topic explains how to create and run a script using UNICOM Intelligence Professional, but you can also create the script using a text editor such as Notepad, and run it using the mrScript Command Line Runner. The content of the script is the same whichever method you use, but additional features are available in UNICOM Intelligence Professional to help you create and run the script.
1 Open UNICOM Intelligence Professional.
2 From the menu, choose File > New > File
3 Click the mrScriptBasic File icon
![](../Desktop%20User%20Guides/images/mrsfileicon.jpg)
4 Type MyFirstTable.mrs in the File Name field, then click Open.
This creates a new file in UNICOM Intelligence Professional.
5 On the first line of the new file, type:
' My first table
Beginning the line with an apostrophe (') indicates that it is a comment. Everything after the apostrophe is ignored when you run the script. Note that any completely blank lines are also ignored, so as you create your script you can insert extra blank lines to make it easier to read.
6 On a new line, type:
Dim TableDoc
The first word,
Dim, is highlighted as you type it, because UNICOM Intelligence Professional recognizes it as a keyword. The
Dim statement declares a new variable, in this case
TableDoc, for use in your script. For more information about the Dim statement, see
Variables.
7 On a new line, type:
Set TableDoc = CreateObject("TOM.Document")
This time, two keywords,
Set and
CreateObject, are highlighted as you type them. This line of script does two things: it creates a new Table Object Model Document object (“TOM.Document”), and it assigns this object to the new
TableDoc variable that you declared in the previous line. This means that from now on in your script, the variable
TableDoc represents a Document object, and all the properties and methods for this type of object are available whenever you use it. For more information on the
Set statement, see
Assignment statements.
8 Next, type the following lines exactly as shown:
TableDoc.DataSet.Load("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\ Data\Xml\museum.mdd", _
, "[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\ Data\Xml\museum.xml", _
"mrXmlDsc")
As you type the dot after TableDoc, a list appears. This is the UNICOM Intelligence Professional ScriptAssist feature. As you have now set the TableDoc variable to represent a Document object, ScriptAssist recognizes the variable as such and suggests the properties and methods that are available for it. You can select the DataSet property from the list by double-clicking it, rather than typing it in. The ScriptAssist feature will appear again as you continue to build your script.
Although the above segment of script appears on three separate lines, it actually represents a single line of script:
TableDoc.DataSet.Load("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\ Data\Xml\museum.mdd", ,
"[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\ Data\Xml\museum.xml", "mrXmlDsc")
As the line is very long, we have split it into three to make it easier to read using the line continuation indicator, a space followed by an underscore ( _), immediately before each line break.
Note Ensure that the underscore is the final character on the line; if any character follows the underscore, even a space, the line will produce an error when you run the script. Also note that as all the lines in this section are part of a single line of script, you cannot insert blank lines between them.
The DataSet.Load method loads the survey data required to create the table. The three parameters give the name and location of the Museum sample metadata (.mdd) file and case data (.xml) file and specify that the XML CDSC is used to read the case data. In fact only the first parameter (which specifies the name and location of the metadata file) is required. For example, you could simply put:
TableDoc.DataSet.Load("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\ Data\XML\museum.mdd")
When you do not specify the parameters for the XML file and the CDSC, this information is automatically picked up from the default data source property in the metadata file. This is all that is required if you have installed the sample data in the default location, because the default data source in the Museum sample .
mdd file points to the case data file in the default location. However, if the sample data has been installed in another location, you must specify the XML and CDSC parameters as shown in the sample script, or change the location specified in the .
mdd file. See
Editing the default data source information in the Museum.mdd file for more information. Therefore, it is a good idea to include these parameters in your scripts, to make them easier to update if you change the data set file name or path.
9 On a new line, type:
TableDoc.Tables.AddNew("Table1", "age * gender", "My First Table")
The Tables.AddNew method creates a new table. The first parameter is the table name, the second defines the structure, and the third is the table description. In this example, the syntax of the table structure is age * gender, which specifies that the age variable forms the side axis (the rows of the table) and the gender variable forms the top axis (the columns of the table).
10 On a new line, type:
TableDoc.Populate()
This line generates the table structure and calculates the values for each cell of the table you have specified.
11 On a new line, type:
TableDoc.Save("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\ Output\MyFirstTable.mtd")
This line saves all the information about the table document, including the data source details, details of the table specification, and the results, to a file.
12 Type the following lines:
With TableDoc.Exports.mrHtmlExport
.Properties["Interactive"] = True
.Properties["LaunchApplication"] = True
.Properties["DisplayOption"] = "Table Only"
.Export("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\
Output\MyFirstTable.htm")
End With
This block of script sets a number of properties to configure the export and exports the table in HTML format to the file MyFirstTable.htm.
The With...End With structure is a compact way of setting a number of properties and methods that apply to the same object, and is the equivalent of writing the following lines, which specify each property and method individually:
TableDoc.Exports.mrHTMLExport.Properties["Interactive"] = True
TableDoc.Exports.mrHTMLExport.Properties["LaunchApplication"] = True
TableDoc.Exports.mrHTMLExport.Properties["DisplayOption"] = "Table Only"
TableDoc.Exports.mrHTMLExport.Export("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\\Output\MyFirstTable.htm")
The complete script should now look like this:
' My first table
Dim TableDoc
Set TableDoc = CreateObject("TOM.Document")
TableDoc.DataSet.Load("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\ Data\XML\museum.mdd", _
, "[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\ Data\XML\museum.xml", _
"mrXmlDsc")
TableDoc.Tables.AddNew("Table1", "age * gender", "My First Table")
TableDoc.Populate()
TableDoc.Save("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\ Output\MyFirstTable.mtd")
With TableDoc.Exports.mrHtmlExport
.Properties["Interactive"] = True
.Properties["LaunchApplication"] = True
.Properties["DisplayOption"] = "Table Only"
.Export("[INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\
Output\MyFirstTable.htm")
End With
13 Press Ctrl+S to save the file, and select a suitable location to save it in. If you want to save it in the same folder as the supplied sample files you will need to choose another file name, as the sample MyFirstTable.mrs file is read-only. Alternatively, you can save the file in a different folder provided that it is on a drive that can access the drive containing the sample data and output folders.
14 Press F5 to run the script.
Table of age by gender
Here is the table produced by the script:
In the Museum sample data set, age and gender are categorical variables. These have a limited number of distinct values or categories, and are typically based on questions that have a predefined set of answers.
For an introduction to market research data types, see
Market research data.
Next
See also